package xyz.xpman.prisonerfitness.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.config.AppConfig;
import xyz.xpman.prisonerfitness.util.FileUtil;
import xyz.xpman.prisonerfitness.util.MediaUtil;
import xyz.xpman.prisonerfitness.util.ScreenUtil;
import xyz.xpman.prisonerfitness.util.ToastUtil;

public class TrainingMusicSettingActivity extends Activity {
    private SeekBar seekBar;
    private TextView percent;
    private TextView selectMusic;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_music_setting);

        initPopWindow();
        initView();
        initEvent();
    }

    private void initPopWindow() {
        // 设置窗口宽度，高度则自适应
        Point windowsSize = ScreenUtil.getWindowsSize(this);
        int width = (int)((float) windowsSize.x * 0.85);
        getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView() {
        percent = findViewById(R.id.percent);
        seekBar = findViewById(R.id.seekBar);
        selectMusic = findViewById(R.id.select_music);


        setSelectMusicName(AppConfig.getBgMusicFileName());
        percent.setText(String.valueOf(AppConfig.getVolume()));
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initEvent() {
        selectMusic.setOnClickListener(view-> {
            Uri uri = Uri.parse(AppConfig.getBgMusicFileDir());
            Log.i("training music", "dir: " + AppConfig.getBgMusicFileDir());

            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("oneshot", 0);
            intent.putExtra("configchange", 0);
            intent.setDataAndType(uri, "audio/*");


            startActivityForResult(intent, 1);
        });



        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                percent.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });


        // 取消：关闭弹窗
        findViewById(R.id.cancel).setOnClickListener(view-> finish());


        // 确定：保存时间后关闭弹窗
        findViewById(R.id.confirm).setOnClickListener(view-> {
            int progress = seekBar.getProgress();
            AppConfig.setVolume(progress);

            if (AppConfig.save()) {
                finish();
            } else {
                ToastUtil.showText(this, "保存失败！");
            }
        });
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 选择音乐
        if (1 == requestCode && RESULT_OK == resultCode) {
            setSelectMusicName(data.getData());
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setSelectMusicName(final Uri uri) {
        String realFilePath = MediaUtil.getRealPath(this, uri);
        Log.i("training music", "fileName: " + realFilePath);

        setSelectMusicName(realFilePath);
        AppConfig.setBgMusicFileName(realFilePath);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setSelectMusicName(final String fileName) {
        selectMusic.setText(FileUtil.getSimpleFileName(fileName));
    }
}